﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PBOffice.Models;
using PBOffice.BLL;

public partial class PersonalManager_Tree : System.Web.UI.Page
{
    List<Department> deptList = new DeptManager().GetAllDepartment();
    protected void Page_Load(object sender, EventArgs e)
    {
        this.TreeView1.Attributes.Add("onclick", "");
        if (!IsPostBack)
        {

            string bc = null;
            foreach (Department item in deptList)
            {
                if (bc == null)
                {
                    bc = item.Branch.BranchShortName;
                    TreeNode tn = new TreeNode("&nbsp;&nbsp;" + item.Branch.BranchShortName);
                    tn.Value = item.Branch.BranchId.ToString();
                    tn.ImageUrl = "~/imges/dept_tree.jpg";
                    this.TreeView1.Nodes.Add(tn);
                    InitTreeByList(tn);
                    continue;
                }
                if (bc != item.Branch.BranchShortName)
                {
                    bc = item.Branch.BranchShortName;
                    TreeNode tn = new TreeNode("&nbsp;&nbsp;" + item.Branch.BranchShortName);
                    tn.Value = item.Branch.BranchId.ToString();
                    tn.ImageUrl = "~/imges/dept_tree.jpg";
                    this.TreeView1.Nodes.Add(tn);
                    InitTreeByList(tn);
                }
            }
        }
    }
    private void InitTreeByList(TreeNode TN)
    {
        foreach (Department item in deptList)
        {
            if (item.ParentId.ToString() == TN.Value)
            {
                TreeNode tn = new TreeNode();
                tn.Text = "&nbsp;&nbsp;" + item.DeptName;
                tn.Value = item.DeptId.ToString();
                //tn.NavigateUrl = item.NodeURL;
                tn.ImageUrl = "~/imges/dept_tree_block.jpg";
                TN.ChildNodes.Add(tn);
                InitTreeByList(tn);
            }


        }
    }
    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        
    }
}